package com.visitor.web.controller;

import com.github.pagehelper.PageInfo;
import com.visitor.bean.SmartUser;
import com.visitor.bean.Visitor;
import com.visitor.service.SmartUserService;
import com.visitor.service.VisitorService;
import com.visitor.utils.IpUtils;
import com.visitor.web.vm.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Api(tags="预约管理")
@Controller
@RequestMapping(value = "visitorManager")
public class VisitorController {
	@Autowired
	private Environment environment;
	@Autowired
	private VisitorService visitorService;
	@Autowired
	private SmartUserService smartUserService;

	@ApiOperation(value="按照编号删除预约信息",notes="按照编号删除预约信息")
	@ApiImplicitParam(name="id",value="预约编号",paramType="path",required=true)
	@ResponseBody
	@PostMapping("/delVisitor/{id}")
	public Response<String> delUser(@PathVariable("id")Long id){
		visitorService.deleteUserById(id);
		return Response.ok("success");
	}

	@ApiOperation(value="按照编号删除预约信息",notes="按照编号删除预约信息")
	@ApiImplicitParam(name="id",value="预约编号",paramType="path",required=true)
	@ResponseBody
	@PostMapping("/updateVisitor")
	public Response<String> updateAdmin(Visitor visitor){
		return visitorService.saveOrUpdate(visitor);
	}

	@ApiOperation(value="分页查找所有预约",notes="分页查找所有预约")
	@ApiImplicitParams({
		@ApiImplicitParam(name="pageNum",value="当前页码",defaultValue="1"),
		@ApiImplicitParam(name="pageSize",value="每页显示数据个数(0是全部)",defaultValue="10")
	})

	@GetMapping("/visitorList")
	public ModelAndView visitorList(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
									@RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
									String shows,ModelAndView modelAndView) {
		PageInfo<Visitor> data= visitorService.getAllByPage(pageNum, pageSize);
		if(null!=shows&&!"".equals(shows)){
			visitorService.updateShow();
			modelAndView.addObject("num",0);
		}
		modelAndView.addObject("page",data);
		modelAndView.setViewName("visitor/visitor-list");
		return modelAndView;
	}

	/**
	  * @Description(功能描述): 预约访客地址
	  **/
	@ResponseBody
	@GetMapping("/visitorUrl")
	public Response<String> visitorUrl() {
		String port=environment.getProperty("server.port");
		String url="http://"+IpUtils.getLocalIpAddr()+":"+port+"/visitorManager/subscribe";
		return Response.ok(url);
	}

	/**
	  * @Description(功能描述): 预约页面
	  **/
	@GetMapping("/subscribe")
	public ModelAndView visitorUrl(ModelAndView modelAndView) {
		modelAndView.setViewName("visitor/subscribe");
		return modelAndView;
	}

	@GetMapping("/subscribeList/{phone}")
	public ModelAndView subscribeList(@PathVariable("phone")String phone, ModelAndView modelAndView) {
		List<Visitor> list= visitorService.findVisitorList(phone);
		modelAndView.addObject("list",list);
		modelAndView.setViewName("visitor/subscribeList");
		return modelAndView;
	}

	//跳转添加预约页面
	@GetMapping("/visitor-add")
	public ModelAndView visitorAddHtml(ModelAndView modelAndView) {
		modelAndView.setViewName("visitor/visitor-add");
		return modelAndView;
	}

	@ApiOperation(value="添加预约",notes="添加预约")
	@ResponseBody
	@PostMapping("/addVisitor")
	public Response<String> addVisitor(Visitor visitor){
		SmartUser smartUser=new SmartUser();
		smartUser.setPhone(visitor.getUserPhone());
		smartUser.setName(visitor.getUserName());
		smartUser=smartUserService.login(smartUser);
		if(null!=smartUser){
			return visitorService.saveOrUpdate(visitor);
		}else{
			return Response.error(300);//查无一人
		}
	}
	//跳转编辑预约页面
	@GetMapping("/visitor-edit")
	public ModelAndView visitorEditHtml(Long id,ModelAndView modelAndView) {
		Visitor visitor=visitorService.findUserById(id);
		modelAndView.addObject("visitor",visitor);
		modelAndView.setViewName("visitor/visitor-edit");
		return modelAndView;
	}

}
